﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.RouteSetting
{
    public partial class FrmRouteChannel : Form
    {
        public dl_route_config_vo RouteConfig { get; set; }
        public FrmRouteChannel()
        {
            InitializeComponent();

            Init();
        }

        private void Init()
        {
            var ret_server = HttpHelper.Get<HResult<List<dl_server_config>>>(AppSettings.Setting.API.GetDownloadServerConfig);

            if (ret_server.success)
            {
                foreach (var server in ret_server.data)
                {
                    TreeNode node = new TreeNode();
                    node.Text = server.storage_server_name;
                    node.Tag = server;

                    tvwAllChannel.Nodes.Add(node);

                    var ret_ch = HttpHelper.Get<HResult<List<dl_room_config>>>(AppSettings.Setting.API.GetRoomConfigList + "?serverId=" + server.id);

                    if (ret_ch.success)
                    {
                        foreach (var ch in ret_ch.data)
                        {
                            TreeNode nc = new TreeNode();
                            nc.Text = ch.channel_name;
                            nc.Tag = ch;
                            nc.Name = ch.server_id + "_" + ch.channel_id;

                            node.Nodes.Add(nc);
                        }
                    }

                }
            }

            tvwAllChannel.ExpandAll();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (this.tvwAllChannel.SelectedNode != null && this.tvwAllChannel.SelectedNode.Level ==1)
            {
                if (this.RouteConfig == null)
                {
                    this.RouteConfig = new dl_route_config_vo();
                    this.RouteConfig.id = Guid.NewGuid().ToString();
                }
                dl_server_config server = tvwAllChannel.SelectedNode.Parent.Tag as dl_server_config;

                dl_room_config config = tvwAllChannel.SelectedNode.Tag as dl_room_config;

                this.RouteConfig.channel_id = config.channel_id;
                this.RouteConfig.channel_name = config.channel_name;
                this.RouteConfig.server_id = server.id;
                this.RouteConfig.storage_server_ip = server.storage_server_ip;
                this.RouteConfig.storage_server_name = server.storage_server_name;
                this.RouteConfig.storage_server_type = server.storage_server_type;

            }
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
